package cn.biplam.front.component.view.systemManage.task.modelimport;

import cn.biplam.common.bean.md.Buffer2Md;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.MdImpTskCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.view.mdManage.buffer2md.Buffer2MdCompareMain;
import cn.biplam.front.component.view.mdManage.buffer2md.main.Buffer2MdCompareTable;
import cn.biplam.front.component.view.systemManage.task.TaskProcessMain;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.htmlscript.script.AjxScript.Is_SubMitData;
import cn.biplam.front.htmlscript.script.AjxScript.Level;
import cn.biplam.front.template.base.ButtonGroup;
import cn.biplam.front.template.control.Confirm;
import cn.biplam.front.template.control.FlatTable;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.pageFrame.mdManage.MdObject;
import cn.biplam.front.template.pageFrame.mdManage.MdPage;

/**
 * 运行管理-任务管理主页面
 * @author WangSong 2009-7-10上午11:46:40
 * @version 1.0
 */
public class ModelImportTaskManage extends AjxResponse {

	public static final String cId = "8801";
	public static final String aId_Main_Page = "880101";
	public static final String aId_Delete = "880102";
	public static final String aId_Import_TO_Md = "880103";
	public static final String aId_Delete_Opt = "880104";
	public static final String aId_Import_TO_Md_Process = "880105";

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(aId_Main_Page.equals(this.actionId)){
			return this.getMainPage(mUser);
		}if(aId_Delete.equals(this.actionId)){
			return this.deletePage(mUser);
		}if(aId_Delete_Opt.equals(this.actionId)){
			return this.deleteOpt(mUser);
		}if(aId_Import_TO_Md.equals(this.actionId)){
			return this.importToMd(mUser);
		}if(aId_Import_TO_Md_Process.equals(this.actionId)){
			String taskId = params.getByKey("taskId");
			if(taskId==null){
				taskId = recordId;
			}
			return new ModelImportProcess(cId,mUser).get(taskId);
		}else{
			throw new ErrorMessage(" TaskManage.java,action is not found:"+this.actionId+"!");
		}
		
	}
	
	
	/**
	 * 主页面
	 * @param mUser 用户信息
	 * @return
	 * @throws ErrorMessage 
	 */
	public HtmlWindow getMainPage(UserInfo mUser) throws ErrorMessage{
		MdPage mp=new MdPage(cId,sHomeUrl,mUser);
		/** 设置界面标题 */
		mp.setTitleDesc(CaptionComp.get(mUser, "283"));
		/** 设置界面标题logo */
		mp.setTitleLogo("/images/t_import.gif");
		
		HtmlWindow task = new HtmlDiv();
		task.put(new HtmlValue(cId+"r",cId+"r","Buffer2MD:"+recordId));
		getTable(task);
		MdObject mdo = new MdObject(cId+"domain");
		mdo.putTable(task);
		/** 模型导入按钮设置*/
		mdo.putOperator(getOperators(recordId, mUser));

		mp.putManage(mdo.get());
		return mp.get();
	}
	
	/**
	 * 删除确认页面
	 * @param mUser  用户信息
	 * @return 删除确认页面
	 * @throws ErrorMessage
	 */
	private HtmlWindow deletePage(UserInfo mUser) throws ErrorMessage{		
		//删除提示信息
		StringBuilder deleteInfo = new StringBuilder();
		deleteInfo.append("确认删除'");
		deleteInfo.append(getDeletePrompt(recordId));
		deleteInfo.append("'?");
		/** 绑定确认删除脚本*/
		Confirm delete = new Confirm(deleteInfo.toString(),recordId);
		delete.bind(aId_Delete_Opt);
		return delete.get();
	}
	
	/**
	 * 删除操作
	 * @param mUser  用户信息
	 * @return 删除操作提示
	 * @throws ErrorMessage
	 */
	private HtmlWindow deleteOpt(UserInfo mUser) throws ErrorMessage{
		HtmlWindow delete = new HtmlDiv();
		/** 删除任务*/
		deleteImportTask(recordId);
		/** 绑定刷新父页面脚本*/
		delete.bind(AjxScript.getA(cId,aId_Main_Page,"refresh"));
		return delete;
	}
	
	/**
	 * 导入到比较表
	 * @param mUser
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow importToMd(UserInfo mUser) throws ErrorMessage{
		HtmlWindow task = new HtmlDiv();
		Buffer2Md b2md = Buffer2Md.getNewInstance(this.recordId);
		b2md.importBuffer2Md(mUser);
		task.bind(PopupScript.cancel("syspop"+Level.Second));
		task.bind(
				AjxScript.getP(Buffer2MdCompareTable.cId, 
						TaskProcessMain.aId_Process, 
						TaskProcessMain.level, 
						Is_SubMitData.Submit, 
						"模型导入")
						);
		return task;
	}
	
	/**
	 * 表信息
	 * @param manage
	 * @throws ErrorMessage
	 */
	private void getTable(HtmlWindow manage) throws ErrorMessage{
		
		DataFilter filter=new DataFilter();
		OperatorResult sr=Operator.get(MdImpTskCode.Get_All_Task, mUser.getUserID(), filter);
		if(sr!=null && !sr.isNull()){
			/** 表主键 */
			String mainKey=MdStruc.MdImpTaskStruc.TASK_ID;
			/** 表格列对应字段名 */
			String[] tableFields={MdStruc.MdImpTaskStruc.MODEL_TYPE,
								  MdStruc.MdImpTaskStruc.MODEL_THEME,
								  MdStruc.MdImpTaskStruc.TASK_DESC,
								  MdStruc.MdImpTaskStruc.STATUS,
								  MdStruc.MdImpTaskStruc.START_TIME,
								  MdStruc.MdImpTaskStruc.END_TIME,
								  "operator"};
			/** 表格列类型 */
			TablePropertys.Col_Content_Type[] fieldsType={
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.NAME_TEXT,
					TablePropertys.Col_Content_Type.BUTTON
			};
			/** 设置列标题 */
			String[] tableColTitles={"模型类型","根主题名称","任务描述","任务状态","开始时间","结束时间","操作"};
			TablePropertys property=new TablePropertys(tableFields,fieldsType,tableColTitles,sHomeUrl,mUser);
		
			/** 设置模型类型*/
			setModelType(property);
			/** 设置主题*/
			setTheme(property);
			/** 设置任务状态*/
			this.setTaskStauts(property);
			/** 设置操作按钮*/
			this.setOperator(property);
			
			/** 创建表对象 */
			FlowTable st=new FlowTable(cId+"table",mainKey,property); 
			/** 将数据集放入表格 */
			st.put(sr);
			st.setHeight("480");
			manage.put(st.get());
		}else{
			FlatTable ft=new FlatTable(cId+"ft","");
			HtmlWindow text =new HtmlText(null,null,"无信息");
			text.setStyle("text-align","center");
			ft.putTh("模型类型",0,0);
			ft.putTh("根主题名称",0,1);
			ft.putTh("任务描述",0,2);
			ft.putTh("任务状态",0,3);
			ft.putTh("开始时间",0,4);
			ft.putTh("结束时间",0,5);
			ft.putTh("操作",0,6);
			ft.putTd(text, 1, 0,"7","4");
			manage.put(ft.get());
		}
	}
	
	/**
	 * 设置模型类型
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setModelType(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.MdImpTaskStruc.MODEL_TYPE, new ModelImportModelTypeCallBack());
	}
	
	/**
	 * 设置主题
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setTheme(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.MdImpTaskStruc.MODEL_THEME, new ModelImportThemeCallBack());
	}
	
	/**
	 * 设置任务状态
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setTaskStauts(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.MdImpTaskStruc.STATUS, new ModelImportTaskStautsCallBack());
	}
	
	
	/**
	 * 设置操作按钮
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setOperator(TablePropertys property) throws ErrorMessage{
		/**设置操作按钮*/
		property.setCallBack("operator", new ModelImportTaskManageperatorCallBack());
		/** 设置动作*/
		/** 删除*/
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.POP_SUBMIT_PR,
				aId_Delete,null);
		/** 增量比较*/
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.POP_SUBMIT_P,
				Buffer2MdCompareMain.aId,Level.Second.toString());
	}
	
	/**
	 * 任务删除提示信息
	 * @param taskId 任务id
	 * @return 任务说明
	 * @throws ErrorMessage 
	 */
	private String getDeletePrompt(String taskId) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		filter.put(MdStruc.MdImpTaskStruc.TASK_ID, taskId);
		OperatorResult sr = Operator.get(MdImpTskCode.Get_Task_By_ID,mUser.getUserID(), filter);
		if(TOOLS.exist(sr)){
			return sr.getValue(MdStruc.MdImpTaskStruc.TASK_DESC);
		}
		return "";
	}
	
	/**
	 * 删除任务
	 * @throws ErrorMessage 
	 */
	private void deleteImportTask(String taskId) throws ErrorMessage{
		/** 删除buffer,删除比较表信息,删除任务*/
		DataFilter  filter = new DataFilter();
		filter.put(MdStruc.MdImpTaskStruc.TASK_ID,taskId);
		Operator.get(MdImpTskCode.Delete_By_ID, mUser.getUserID(), filter);
	}
	
	/**
	 * 模型导入操作按钮并绑定按钮事件
	 * @param mUser
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow getOperators(String recordId,UserInfo mUser) throws ErrorMessage{
		ButtonGroup operatorButtons=new ButtonGroup("",Float_Type.Left);
		if(recordId!=null){
			HtmlWindow refreshBt=ButtonComp.get(mUser, "200");
			refreshBt.bind(Window_Event.OnClick, AjxScript.getA(cId,aId_Main_Page,"refresh"));
			operatorButtons.put(refreshBt);
		}
		return operatorButtons.get();
	}
	
}
